/********************************************************************************
* File Name:	owner.cpp
* Description:	第7章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "owner.h"
#include "ui_owner.h"
#include <QtSql>
#include <QSqlQuery>
#include <QMessageBox>
#include "addowner.h"
#include "deleteowner.h"
#include "databaseconnection.h"
owner::owner(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::owner)
{
    ui->setupUi(this);
}

owner::~owner()
{
    delete ui;
}

void owner::on_btnSearchAll_clicked()
{
    if(!databaseconnection::Open())
    {
        QMessageBox::about(this,"Message","connect error");
        return;
    }
    else
    {
        QSqlQueryModel *model = new QSqlQueryModel;
        model->setQuery("select * from house.renter");
        model->setHeaderData(0,Qt::Horizontal,tr("ID"));
        model->setHeaderData(1,Qt::Horizontal,tr("renterName"));
        model->setHeaderData(2,Qt::Horizontal,tr("contact"));
        model->setHeaderData(3,Qt::Horizontal,tr("contractId"));
        model->setHeaderData(4,Qt::Horizontal,tr("price"));
        model->setHeaderData(5,Qt::Horizontal,tr("remark"));
        ui->tbvowner->setModel(model);
        ui->tbvowner->show();
    }
    databaseconnection::Close();
}

void owner::on_btnCancel_clicked()
{
    this->close();
}

void owner::on_btnAdd_clicked()
{
    addowner *ado = new addowner();
    ado->show();
}

void owner::on_btnSearchOwner_clicked()
{
    QString renterId = ui->leSearchOwner->text().trimmed();
    if(!databaseconnection::Open())
    {
        QMessageBox::about(this,"Message","connect error");
    }
    else
    {
        QSqlQueryModel *model = new QSqlQueryModel;
        model->setQuery("select * from house.renter where renterId='"+renterId+"'");
        model->setHeaderData(0,Qt::Horizontal,tr("ID"));
        model->setHeaderData(1,Qt::Horizontal,tr("renterName"));
        model->setHeaderData(2,Qt::Horizontal,tr("contact"));
        model->setHeaderData(3,Qt::Horizontal,tr("contractId"));
        model->setHeaderData(4,Qt::Horizontal,tr("price"));
        model->setHeaderData(5,Qt::Horizontal,tr("remark"));
        ui->tbvowner->setModel(model);
        ui->tbvowner->show();
    }
    databaseconnection::Close();
}

void owner::on_btnDel_clicked()
{
    DeleteOwner *delO = new DeleteOwner();
    delO->show();
}
